package mn.slide.laborberlin.tabgroup;

import java.util.ArrayList;

import mn.slide.laborberlin.db.DataLayer;
import mn.slide.laborberlin.main.DetialActivity;
import mn.slide.laborberlin.main.HomeActivity;
import mn.slide.laborberlin.main.R;
import mn.slide.laborberlin.merklist.MerklisteActivity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class FavoriteGroupActivity extends ActivityGroup {

	private ArrayList<View> history;
	public static FavoriteGroupActivity group;
	Animation outToLeft;
	Animation inFromRight;
	Animation inFromLeft;
	Animation outToRight;
	Button back_btn;
	Button add_btn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		group = this;
		history = new ArrayList<View>();

		add_btn = (Button) getParent().findViewById(R.id.add);
		View view = getLocalActivityManager().startActivity(
				"ListView",
				new Intent(this, MerklisteActivity.class)
						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
				.getDecorView();
		replaceView(view);

	}

	public void goTo(Intent intent) {
		View view = getLocalActivityManager().startActivity("ListView",
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
		replaceView(view);

	}

	public void replaceView(View v) {
		outToLeft = AnimationUtils.loadAnimation(this, R.anim.out_to_left);
		inFromRight = AnimationUtils.loadAnimation(this, R.anim.in_from_right);

		if (history.size() > 0) {
			View v1 = history.get(history.size() - 1);
			v1.setAnimation(outToLeft);
			v.setAnimation(inFromRight);
		}
		history.add(v);
		checkBackBtn();
		setContentView(v);

	}

	public void back() {
		if (history.size() > 1) {
			add_btn = (Button) getParent().findViewById(R.id.add);
			inFromLeft = AnimationUtils
					.loadAnimation(this, R.anim.in_from_left);
			outToRight = AnimationUtils
					.loadAnimation(this, R.anim.out_to_right);
			View v = history.get(history.size() - 1);
			v.setAnimation(outToRight);
			history.remove(history.size() - 1);
			v = history.get(history.size() - 1);
			v.setAnimation(inFromLeft);
			if (DataLayer.layer.detialMerk == 1) {
				Animation out = AnimationUtils.loadAnimation(this,
						R.anim.pop_up_sms_out);
				add_btn.setAnimation(out);
				add_btn.setVisibility(View.GONE);
				DataLayer.layer.detialMerk = 0;
			} else if (DataLayer.layer.detialMerk == 2) {

				Animation in = AnimationUtils.loadAnimation(this,
						R.anim.pop_up_sms_in);
				add_btn.setAnimation(in);
				add_btn.setVisibility(View.VISIBLE);
				DataLayer.layer.detialMerk = 1;
			}
			checkBackBtn();
			setContentView(v);
		} else {
			finish();
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (DataLayer.layer.detialMerk != 1) {
			add_btn.setVisibility(View.INVISIBLE);
		} else {
			add_btn.setVisibility(View.VISIBLE);
		}
		if (history.size() > 1) {
			back_btn.setVisibility(View.VISIBLE);
		}
	}

	public void checkBackBtn() {
		back_btn = (Button) getParent().findViewById(R.id.back_btn);
		Animation in = AnimationUtils.loadAnimation(this, R.anim.pop_up_sms_in);
		Animation out = AnimationUtils.loadAnimation(this,
				R.anim.pop_up_sms_out);
		if (history.size() == 1 && back_btn.getVisibility() == View.VISIBLE) {
			back_btn.setAnimation(out);
			back_btn.setVisibility(View.INVISIBLE);
		} else if (history.size() == 2) {
			back_btn.setAnimation(in);
			back_btn.setVisibility(View.VISIBLE);
		} else {
			back_btn.clearAnimation();
		}

	}

	@Override
	public void onBackPressed() {
		final LinearLayout add_bg_lout = (LinearLayout) getParent()
				.findViewById(R.id.add_bg_lout);

		if (add_bg_lout.getVisibility() == View.VISIBLE) {
			LinearLayout addBtnBg = (LinearLayout) getParent().findViewById(
					R.id.add_btn_bg);
			Animation outToBottm = AnimationUtils.loadAnimation(this,
					R.anim.out_to_bottom);
			addBtnBg.setAnimation(outToBottm);
			addBtnBg.setVisibility(View.GONE);
			outToBottm.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onAnimationRepeat(Animation animation) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onAnimationEnd(Animation animation) {
					add_bg_lout.setVisibility(View.GONE);
				}
			});
		} else {
			back();
		}
	}

}
